package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.FriendsListAdapter;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.InviteFriendListener;
import com.yksj.healthtalk.ui.chatting.InviteAttentionActivity.ViewHolder;
import com.yksj.healthtalk.utils.DataParseUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 要求身边的人
 * @author zhao
 *
 */
public class InviteByNearbyFragment extends Fragment implements InviteFriendListener{
	LocationClient mLocationClient;//获取位置
	ListView mListView;
	FriendsListAdapter mAdapter;
	private int pageSize =1;
	private int pageNum = 20;
	private double al;
	private double lt;
	private PullToRefreshListView mPullToRefreshListView;
	
	final ObjectHttpResponseHandler mHandler = new ObjectHttpResponseHandler(getActivity()) {
		@Override
		public Object onParseResponse(String cotent) {
			return DataParseUtil.parseInviteResultData(cotent);
		}
		public void onSuccess(int statusCode, Object response) {
			if(response instanceof String){
				//邀请错误
				ToastUtil.showShort(getActivity(), String.valueOf(response));
			}else{
				mAdapter.onDataChange((List<CustomerInfoEntity>)response);
			}
		};
		public void onFinish() {
			mPullToRefreshListView.onRefreshComplete();
		};
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.pull_to_refresh_for_end_layout,null);
		mPullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.listview);
		mListView = mPullToRefreshListView.getRefreshableView();
		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				onQueryData(++pageSize,pageNum,al,lt);
			}
			
		});
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				ViewHolder holder = (ViewHolder)view.getTag();
				boolean  isChecked = holder.chooseBox.isChecked();
				CustomerInfoEntity entity = mAdapter.getItem(position-1);
				String userid = entity.getId();
				mAdapter.onChoose(userid,!isChecked);
				holder.chooseBox.setChecked(!isChecked);
			}
		});
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mAdapter = new FriendsListAdapter(getLayoutInflater(savedInstanceState),getActivity());
		mListView.setAdapter(mAdapter);
		initRequestLocation();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(mLocationClient != null && mLocationClient.isStarted()){
			mLocationClient.stop();
		}
		mLocationClient = null;
		
	}
	
	/**
	 * 邀请身边好友
	 * @param v	
	 */
	private void initRequestLocation(){
		if(mLocationClient == null){
			mLocationClient = new LocationClient(getActivity());
			mLocationClient.registerLocationListener(new BDLocationListener() {
				@Override
				public void onReceivePoi(BDLocation arg0) {
					onReceiveLocation(arg0);
				}
				@Override
				public void onReceiveLocation(BDLocation db) {
					if(mLocationClient != null){
						mLocationClient.stop();
						mLocationClient.unRegisterLocationListener(this);
					}
					double la = db.getLatitude();
					double lt = db.getLongitude();
					onQueryData(pageSize,pageNum,la, lt);
				}
			});
			LocationClientOption clientOption = new LocationClientOption();
			clientOption.setOpenGps(true);
			clientOption.setCoorType("gps");
			clientOption.setPoiNumber(3);
			clientOption.setAddrType("detail");
			clientOption.setServiceName("com.baidu.location.service_v3.3");
			clientOption.setPoiExtraInfo(true);
			clientOption.setPriority(LocationClientOption.NetWorkFirst);
			clientOption.disableCache(true);
			clientOption.setPoiDistance(1000);
			clientOption.setScanSpan(6000);
			mLocationClient.setLocOption(clientOption);
		}
		mLocationClient.start();
		mLocationClient.requestLocation();
	}
	
	private void onQueryData(int pageSize,int pageNum ,double al,double lt){
		HttpRestClient.doHttpInviteByNearby(SmartFoxClient.getLoginUserId(), lt, al, pageSize, pageNum, mHandler);
	}

	@Override
	public List<String> onInvite() {
		return new ArrayList<String>(mAdapter.mChoosedMap.keySet());
	}
}
